﻿Namespace DGZfP.EventPublisher
    Public Class SchedulerClient
        Inherits DotNetNuke.Services.Scheduling.SchedulerClient

#Region "Constructors"

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Constructs a Event SchedulerClient
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <param name="objScheduleHistoryItem">A SchedulerHistiryItem</param>
        ''' -----------------------------------------------------------------------------
        Public Sub New(ByVal objScheduleHistoryItem As DotNetNuke.Services.Scheduling.ScheduleHistoryItem)
            MyBase.new()
            Me.ScheduleHistoryItem = objScheduleHistoryItem
        End Sub

#End Region
        Private Shared Function SynchronizeNextEventSource()
            Dim source As SourceInfo = SourceController.GetNextSourceToRun

            Dim master = New DGZfP.EventPublisher.Calendar.EventsByDataTable(SourceProvider.GetData(source), source.Context)
            master.ColumnsMapping(DGZfP.EventPublisher.Calendar.EventByDataRow.IdColumn) = source.Mapping_ID
            master.ColumnsMapping(DGZfP.EventPublisher.Calendar.EventByDataRow.StartColumn) = source.Mapping_Start
            master.ColumnsMapping(DGZfP.EventPublisher.Calendar.EventByDataRow.EndColumn) = source.Mapping_End
            master.ColumnsMapping(DGZfP.EventPublisher.Calendar.EventByDataRow.TitleColumn) = source.Mapping_Title
            master.ColumnsMapping(DGZfP.EventPublisher.Calendar.EventByDataRow.ContentColumn) = source.Mapping_Content
            master.ColumnsMapping(DGZfP.EventPublisher.Calendar.EventByDataRow.LocationColumn) = source.Mapping_Location
            master.ColumnsMapping(DGZfP.EventPublisher.Calendar.EventByDataRow.AllDayColumn) = source.Mapping_AllDay

            Dim slave = New DGZfP.EventPublisher.Calendar.EventsByGoogleCalendarFeed( _
                New GoogleCalendarController().GetService(source.Google_Username, source.Google_Password), _
                source.Google_Calendar_ID, master.LowestDate, source.Context)

            slave.SynchroniseEvents(master)
            Return source.ToString()
        End Function
        Public Overrides Sub DoWork()
            Try

                'notification that the event is progressing
                Me.Progressing()    'OPTIONAL
                Dim result As String = SynchronizeNextEventSource()
                Me.ScheduleHistoryItem.Succeeded = True     'REQUIRED
                Me.ScheduleHistoryItem.AddLogNote(String.Format("Events Sync ({0}) completed.", result))

            Catch exc As Exception    'REQUIRED
                Me.ScheduleHistoryItem.Succeeded = False    'REQUIRED
                Me.ScheduleHistoryItem.AddLogNote("Events Sync failed." + exc.ToString)    'OPTIONAL

                'notification that we have errored
                Me.Errored(exc)    'REQUIRED

                'log the exception.
                LogException(exc)    'OPTIONAL
            End Try
        End Sub
    End Class
End Namespace

